#!/bin/bash

# Switching the sdcard between 6.6+ and 6.1
# Usage
# [DEV=<xxx>] switch.sh [-r]
# Default is switching from 6.1 to 6.6+
# -r means we want to restore sdcard to 6.1

source ./envsetup

if [[ -z "${DEV}" ]]; then
	DEV=sdb1
fi

FROM=6.1
TO=6.6

if [ "$1" == "-r" ]; then
	FROM=6.6
	TO=6.1
fi

WS_SG2042=$WS/sg2042

sudo umount $WS/u-disk
sudo mount /dev/$DEV $WS/u-disk/

echo "Restoring sd-card from $FROM to $TO ......"

# cleanup
sudo rm $WS/u-disk/zsbl.bin
sudo rm $WS/u-disk/riscv64/fw_dynamic.bin
sudo rm $WS/u-disk/riscv64/mango-milkv-pioneer.dtb
sudo rm $WS/u-disk/riscv64/riscv64_Image

if [ $TO = "6.6" ]; then
	sudo cp $WS_SG2042/archive/sg2042-master/zsbl.bin $WS/u-disk/
	sudo cp $WS_SG2042/archive/sg2042-master/fw_dynamic.bin $WS/u-disk/riscv64/
elif [ $TO = "6.1" ]; then
	sudo cp $WS_SG2042/archive/sdcard_sdb1-6.1.31/u-disk/zsbl.bin $WS/u-disk/
	sudo cp $WS_SG2042/archive/sdcard_sdb1-6.1.31/u-disk/riscv64/fw_dynamic.bin $WS/u-disk/riscv64/
	sudo cp $WS_SG2042/archive/sdcard_sdb1-6.1.31/u-disk/riscv64/mango-milkv-pioneer.dtb $WS/u-disk/riscv64/
	sudo cp $WS_SG2042/archive/sdcard_sdb1-6.1.31/u-disk/riscv64/riscv64_Image $WS/u-disk/riscv64/
fi

sudo umount $WS/u-disk

echo "Done!"

